/*
 *   Copyright (c) 2010 Sonrisa Informatikai Kft. All Rights Reserved.
 * 
 *  This software is the confidential and proprietary information of
 *  Sonrisa Informatikai Kft. ("Confidential Information").
 *  You shall not disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into
 *  with Sonrisa.
 * 
 *  SONRISA MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 *  THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 *  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 *  PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SONRISA SHALL NOT BE LIABLE FOR
 *  ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 *  DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package com.zh.jbet.web.components.datatable;

import com.zh.jbet.core.entity.BaseEntity;
import com.zh.jbet.web.JBetServiceProvider;
import com.zh.jbet.web.components.ModalEditPanel;
import com.zh.jbet.web.model.EntityDataProvider;
import com.zh.jbet.web.model.EntityModel;
import com.zh.jbet.web.model.IServiceableModel;
import com.zh.jbet.web.util.WicketUtils;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.StringResourceModel;

/**
 *
 * @author sonrisa
 */
public class JBetEditDataPanel<R extends BaseEntity> extends DataTablePanel<R> {
    private Class<? extends ModalEditPanel<R>> editPopupClass;
    private JBetServiceProvider serviceProvider;

    public JBetEditDataPanel(String id, int rowsPerPage, final JBetServiceProvider serviceProvider,
            TableColumnDescriptor[] columns, Class<? extends ModalEditPanel<R>> editPopupClass) {
        super(id, columns, new EntityDataProvider<R>(serviceProvider), rowsPerPage);
        this.editPopupClass = editPopupClass;
        this.serviceProvider = serviceProvider;

        add(new AjaxLink("newEntity") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                editEntity(target, new EntityModel<R>(serviceProvider));
            }
        });
        setOutputMarkupId(true);
    }

    @Override
    public EntityDataProvider<R> getDataProvider() {
        return (EntityDataProvider<R>)super.getDataProvider();
    }
    
    @Override
    protected void onRowSelected(AjaxRequestTarget target, IModel<R> rowModel) {
        editEntity(target, rowModel);
    }

    protected void editEntity(AjaxRequestTarget target, IModel<R> model) {
        ModalEditPanel gcm = WicketUtils.createModalPopupPanel(editPopupClass, getJBetPage().getModal(), createServiceableModel(model));
        gcm.setEditCloseListener(new ModalEditCloseListener() {
            @Override
            public void onModalClose(AjaxRequestTarget target, boolean save, boolean delete) {
                if (save || delete) {
                    target.addComponent(JBetEditDataPanel.this);
                }
                onPanelModalClose(target, save, delete);
            }
        });
        gcm.show(target);
    }

    protected void onPanelModalClose(AjaxRequestTarget target, boolean save, boolean delete) {

    }

    protected IServiceableModel<R> createServiceableModel(IModel<R> model) {
        return (model instanceof IServiceableModel) ? (IServiceableModel<R>)model : new EntityModel<R>(model.getObject(), serviceProvider);
    }
    
    @Override
    protected IModel<String> createRowLinkModel() {
        return new StringResourceModel("link.edit", null);
    }
}
